<?php
class Representatives extends Controller {

    private $roles = array('all','mps','advisors', 'mayors');

    private $language;
    function Representatives() {
        parent::Controller();
        session_start();
        $this->load->helper('my_language');
        $this->load->helper('debug');
        $this->load->model('representatives_model');
        $this->load->model('municipalities_model');
        $this->load->model('izborni_edinici_model');
        $this->lang->load("front");
        $this->language = get_language_id($this->lang->lang());

        if (!$this->session->userdata('admin_logged_in')) redirect(base_url().'mk/admin/login', 'refresh');
    }

    function index() {
        redirect(base_url().'mk/admin/representatives/listrepresentatives', 'refresh');
    }

    function newRepresentative() {
        $content['head'] = $this->load->view('head',null,true);
        $content['header'] = $this->load->view('admin/header',null,true);
        $content['middleFront'] = $this->load->view('admin/addrepresentative',null,true);
        $content['footer'] = $this->load->view('footer',null,true);
        $this->load->view('admin/index_front',$content);
    }

    function addRepresentative() {
        if (isset($_POST['addrep_btn'])) {
            $post_array = $this->cleaninput($input);
//            print_r($post_array); die();
            $data['addSuccess'] = $this->representatives_model->addRepresentative($post_array);
//            print_r($data);die();
        }
        $this->listRepresentatives('all', $data['addSuccess']);
//        exit();
//        $content['head'] = $this->load->view('head',null,true);
//        $content['header'] = $this->load->view('admin/header',null,true);
//        $content['middleFront'] = (isset($data['addSuccess']))?$this->load->view('admin/addrepresentative',$data,true):
//                                                       $this->load->view('admin/addrepresentative',null,true);
//        $content['footer'] = $this->load->view('footer',null,true);
//        $this->load->view('admin/index_front',$content);
    }

//    function listrepresentatives() {
//        $data['representatives'] = $this->representatives_model->getAllRepresentatives();
//        $content['head'] = $this->load->view('head',null,true);
//        $content['header'] = $this->load->view('admin/header',null,true);
//        $content['middleFront'] = $this->load->view('admin/listrepresentatives',$data,true);
//        $content['footer'] = $this->load->view('footer',null,true);
//        $this->load->view('admin/index_front',$content);
//    }

    function listRepresentatives($type = 'all', $success = FALSE) {
        //BITNO: Znaci, ako se klikne na Gradonacalnici ili na Sovetnici, treba soodvetno da se izvadat site. Na frontendot vo tabelata treba da pisuva
        //Opstina, a ako se izbere Pratenici, na treba da pisuva Izborna edinica. Soodvetno se vlecat i pretstavuvaat podatocite
        //ako se izbere Site, treba da smislime shto da pisuva, a treba soodvetno na sekoj red so sekoj predstavnik treba da dade opstina ili izbor.edin.

        $this->lang->load('front');
        if (!in_array($type, $this->roles)) redirect(base_url().$this->lang->lang());

        switch ($type) {
            case 'all':
                $representatives = $this->representatives_model->getAll(1, false);
                break;
            case 'mps':
                $representatives = $this->representatives_model->getAllPratenici(1, false);
                break;
            case 'advisors':
                $representatives = $this->representatives_model->getAllSovetnici(1, false);
                break;
            case 'mayors':
                $representatives = $this->representatives_model->getAllGradonacalnici(1, false);
                break;
        }
        $data['representatives'] = $representatives;
        $data['admin'] = TRUE;
        $data['addSuccess'] = ($success)?$success:FALSE;
        $content['head'] = $this->load->view('head',null,true);
        $content['header'] = $this->load->view('admin/header',null,true);
        $content['middleFront'] = $this->load->view('list_representatives', $data, true);
        $content['footer'] = $this->load->view('footer',null,true);
        $this->load->view('admin/index_front',$content);
    }

    function deleteRepresentative($repID) {
        if ($repID > 0 && is_numeric($repID)) {
            $deleted = $this->representatives_model->deleterepresentative($repID);
            redirect(base_url().'mk/admin/representatives/listrepresentatives');    
        }
        else redirect(base_url().'mk/admin/representatives/listrepresentatives');
    }

    function editRepresentative($repID) {
        $data['editRep'] = TRUE;
        if (isset($_POST['addrep_btn'])) {
            $post_array = $this->cleaninput($input);
//            print_r($post_array); die();
            $data['addSuccess'] = $this->representatives_model->updateRepresentative($post_array, $repID);
//            print_r($data);die();
            $this->listRepresentatives('all', $data['addSuccess']);
        }
        else {
//            echo "da"; die();
            $data['representative'] = $this->representatives_model->getRepresentativeById($repID);
            $data['mun_const_ddm'] = $this->getMunConstEdit($data['representative']->role_id, $data['representative']->mun_const_id);
//            var_dump($data['mun_const_ddm']); die();
        $content['head'] = $this->load->view('head',null,true);
        $content['header'] = $this->load->view('admin/header',null,true);
        $content['middleFront'] = (isset($data['addSuccess']) || isset($data['representative']))?$this->load->view('admin/addrepresentative',$data,true):
                                                       $this->load->view('admin/addrepresentative',null,true);
        $content['footer'] = $this->load->view('footer',null,true);
        $this->load->view('admin/index_front',$content);
        }
    }
        
    function ajaxGetMunicipalities() {
        $role = $this->input->post('role');
        $result = '';
        if ($role === '2' OR $role === '3') {
            $result = '<td>Municipality/City:</td>';
            $result .= '<td><select name = "municipalities">';
            $units =  $this->municipalities_model->getAllMunicipalities(1);
            foreach ($units as $key=>$unit) {
                $result .= '<option value="'.$unit->municipality_id.'">'.$unit->municipality_name.'</option>';
            }
        }
        else {
            $result = '<td>Constituency:</td>';
            $result .= '<td><select name = "municipalities">';
            $units = $this->izborni_edinici_model->getAllIzborniEdinici(1);
//            debug($units); die();
            foreach ($units as $key=>$unit) {
                $result .= '<option value="'.$unit->izborna_edinica.'">'.$unit->izborna_edinica.'</option>';
            }
        }
        $result .= '</select></td>';
        echo $result;
    }

    private function getMunConstEdit($rep_role, $rep_mun_const) {
        if ($rep_role === '2' OR $rep_role === '3') {
            $result = '<td>Municipality/City:</td>';
            $result .= '<td><select name = "municipalities">';
            $units =  $this->municipalities_model->getAllMunicipalities(1);
            foreach ($units as $key=>$unit) {
                $result .= '<option value="'.$unit->municipality_id.'"';
                $result .= ($unit->municipality_id == $rep_mun_const)?'selected = "selected">':'>';
                $result .= $unit->municipality_name.'</option>';
            }
        }
        else {
            $result = '<td>Constituency:</td>';
            $result .= '<td><select name = "municipalities">';
            $units = $this->izborni_edinici_model->getAllIzborniEdinici(1);
            foreach ($units as $key=>$unit) {
                $result .= '<option value="'.$unit->izborna_edinica.'"';
                $result .= ($unit->izborna_edinica == $rep_mun_const)?'selected = "selected">':'>';
                $result .= $unit->izborna_edinica.'</option>';
            }
        }
        $result .= '</select></td>';
        return $result;
    }

    private function cleaninput(&$input) {
        foreach ($_POST as $key=>$val) {
            $input[$key] = $val;
        }
        return $input;
    }

//    function

}
